import {
  updateCount,
  updateSelected,
  getCartList
} from '../../api/cart.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TitleBarHeight: app.globalData.TitleBarHeight,
    cartList: [], // 购物车列表
    goodsList: [],
    totalPrice: 0.00
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()
  },
  async init() {
    const data = await getCartList()
    let selectCount = 0 // 所有商品选中计数，判断是否全选
    let cartNum = 0
    const dealData = data.validListShop.map(t => {
      let cartSelectedCount = 0
      cartNum += t.cart ? t.cart.length : 0
      t.cart.map(u => {
        if (u.selected) {
          cartSelectedCount++
          selectCount++
        }
        u.sku.price = u.sku.price && (u.sku.price / 100).toFixed(2)
        return u
      })
      t.selected = cartSelectedCount === t.cart.length && t.cart.length > 0
      return t
    })
    this.setData({
      selectedAll: selectCount > 0 && selectCount == cartNum,
      cartList: dealData
    })
  },
  getCartInfo(e) {
    const {
      totalPrice,
      cartList
    } = e.detail
    // this.data.cartList = cartList
    this.setData({
      totalPrice
    })
  },
  async updateAllSelect(e) {
    const selected = e.detail

    // let arr = JSON.parse(JSON.stringify(this.data.cartList))
    // arr.map(t => {
    //   t.cart.map(u => {
    //     if (selected) {
    //       t.selected = true
    //       u.selected = true
    //     } else {
    //       t.selected = false
    //       u.selected = false
    //     }
    //   })
    // })

    // this.setData({
    //   cartList: arr
    // })

    let idsArr = []
    this.data.cartList.map(uu=> {
      uu.cart.map(pp=>{
        idsArr.push(pp.id)
      })
    })

    const params = {
      ids: idsArr,
      selected
    }
    const res = await updateSelected(params)
    if (res) {
      this.init()
    }
  },
  runPay() {
    // let allTotalPrice = 0;
    // let dealData = this.data.cartList.map(ttt => {
    //   ttt.cart.map((uuu, yyy) => {
    //     if (!uuu.selected) {
    //       ttt.cart.splice(yyy, 1)
    //     } else {
    //       uuu.sku.totalPrice = (parseFloat(uuu.sku.price) * ttt.count).toFixed(2)
    //       allTotalPrice += parseFloat(uuu.sku.totalPrice)
    //     }
    //   })

    //   return ttt
    // })

    // const storageData = {
    //   allTotalPrice: allTotalPrice.toFixed(2),
    //   cartList: [...dealData]
    // }

    let dealData = []
    this.data.cartList.map(yy => {
      yy.cart.map(uu => {
        // obj.cartId = uu.id
        // obj.skuId = uu.sku.id
        // obj.count = uu.count
        dealData.push({
          cartId: uu.id
        })
      })
    })
    wx.setStorageSync('cartItems', dealData)

    wx.navigateTo({
      url: '/pages/confirmOrder/index',
    });
  },
  // 商品数量增减触发接口更新
  async updateCartCount(e) {
    const {id, count = null} = e.detail
    const params = {
      id,
      count
    }
    const res = await updateCount(params)
    if (res) {
      this.init()
    }
  },
  // 商品选中or反选or全选触发接口更新
  async updateCartSelected(e){
    const {ids, selected} = e.detail
    const params = {
      ids,
      selected
    }
    const res = await updateSelected(params)
    if (res) {
      this.init()
    }
  }
})